<template>
  <div class="login-container">
    <div class="login-form-container">
      <div class="login-title">
        <h2>积分管理系统</h2>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" class="login-button" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
        ],
      },
      loading: false,
    }
  },
  methods: {
    ...mapActions('user', ['login']),

    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.login(this.loginForm)
            .then(() => {
              this.$message.success('登录成功')
              this.$router.push('/')
            })
            .catch(error => {
              this.$message.error(error.message || '登录失败')
            })
            .finally(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style scoped>
.login-container {
  height: 100%;
  width: 100%;
  background-color: #f5f5f5;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form-container {
  width: 400px;
  padding: 40px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}

.login-title h2 {
  font-size: 24px;
  color: #1a1a1a;
  font-weight: 700;
  margin-bottom: 10px;
}

.login-subtitle {
  font-size: 16px;
  color: #808080;
  font-weight: 400;
  margin-bottom: 30px;
}

.login-form {
  margin-top: 20px;
}

.el-form-item {
  margin-bottom: 24px;
}

.el-form-item__label {
  font-size: 14px;
  font-weight: 600;
  color: #4d4d4d;
}

.el-input {
  height: 40px;
}

.el-input__inner {
  height: 40px;
  line-height: 40px;
  background-color: #f7f7f7;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  color: #333333;
}

.el-input__inner::placeholder {
  color: #999999;
}

.login-button {
  width: 100%;
  height: 40px;
  background-color: #1c78f2;
  border-color: #1c78f2;
  font-size: 16px;
  font-weight: 600;
}

.login-button:hover {
  background-color: #1a6edb;
  border-color: #1a6edb;
}
</style> 